<!-- 正文开始 -->
<div class="layui-fluid" id="vitalSignFrame">
    <div class="layui-card">
        <div class="layui-card-body">
            <div class="layui-form toolbar" lay-filter="vitalSignFrame">
                <div class="layui-form-item">
                    <label class="layui-label">科室</label>
                    <div class="layui-inline">
                        <select name="deptCode" lay-filter="vitalSignFrame-deptCode">
                            <option value="" data-default="">所有科室</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn btnSearch"><i class="layui-icon">&#xe615;</i>搜索</button>
                    </div>
                    <label class="layui-label">第几周</label>
                    <div class="layui-inline" style="width:100px;">
                        <select name="weekNo" lay-filter="vitalSignFrame-weekNo">
                            <option value="" data-default=""></option>
                        </select>
                    </div>
                </div>
            </div>

            <table border="0" cellpadding="0" cellspacing="0" style="background-color: white;">
                <tr align="left" valign="top">
                    <td>
                        <table class="layui-table" id="vitalSignFrameTable" lay-filter="vitalSignFrameTable"></table>
                    </td>
                    <td>
                        <div id="vitalSign"></div>
                    </td>
                </tr>
            </table>

        </div>
    </div>
</div>
<style>
    #vitalSign{
        margin:0;
        width:1000px;
        height:1414px;
    }
</style>
<script src="views/patient/VitalSignUtil.min.js?v=1" charset="UTF-8"></script>

<script>
    layui.use(['table', 'form', 'http', 'tools','zrender','zdraw'], function () {
        var $ = layui.jquery;
        var table = layui.table;
        var form = layui.form;
        var http = layui.http;
        var tools = layui.tools;

        var viewId = "vitalSignFrame";
        var $view = $('#'+viewId);
        var $deptCode = $view.find("select[name='deptCode']");
        var $weekNo = $view.find("select[name='weekNo']");
        var dateList=[],CurrentRowData={};

        tools.select.deptCodeOptions($deptCode);
        form.render('select',viewId);

        var fnGetSearchParam = function(){
            var param = {};
            param.deptCode = $deptCode.val();
            return param;
        };

        var fnRender = function() {
            //渲染表格
            var vitalSignFrameTableIns = table.render({
                id: 'vitalSignFrameTable',
                elem: '#vitalSignFrameTable',
                method: 'get',
                //contentType: "application/json",
                url: http.base_server + 'vitalSign/getPatientList',
                where: fnGetSearchParam(),
                page: false,
                cellMinWidth: 50,
                height: 'full-200',
                width:280,
                // limits : [10,20,30,40,50],
                // limit : 20,
                // autoSort: false,
                // request: {
                //     pageName: 'pageNum',
                //     limitName: 'pageSize'
                // },
                cols: [[
                    // {type: 'numbers'},
                    {field: 'bedName2', sort: false, title: '床号', width: 60},
                    {field: 'code', sort: false, title: '病人号', width: 80},
                    {field: 'name', sort: true, title: '姓名', width: 80}
                ]],
                done: function(res){
                  if(res && res.msg){
                      http.okMsg(res.msg);
                  }
                },
                error: function (code, msg) {
                    http.fail(code, msg);
                }
            });

            var fnSearch = function () {
                var param = fnGetSearchParam();
                vitalSignFrameTableIns.reload({where: param,page: {curr: 1}});
            };

            // 搜索按钮点击事件
            $view.find(".btnSearch").click(function () {
                fnSearch();
            });

            form.on("select(vitalSignFrame-deptCode)",function(data){
                fnSearch();
            });

        };
        fnRender();
        var fnRenderVitalSign = function(data){
            var vsUtil = new VitalSignUtil(data);
            vsUtil.doDraw({elem:'vitalSign',x: 10, y: 10});
        };

        table.on('row(vitalSignFrameTable)',function(obj){
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            var data = obj.data;
            CurrentRowData = data;

            if(!data){return;}
            var param = {patientCode:data.code,patientCureNo:data.cureNo};
            http.get('vitalSign/getOpDateList',param,function(res){
                if(res.ok){
                    if(res.data){
                        dateList = res.data;
                    }else{
                        dateList = [];
                    }
                }else{
                    dateList = [];
                }
                var weekCount = Math.ceil(dateList.length/7);
                var weeks = [];
                for(var i=1;i<=weekCount;i++){
                    weeks.push(i);
                }
                tools.select.options($weekNo,weeks);
                form.render('select',viewId);
            });
            http.post('vitalSign/getVitalSignList',param,function(res){
               if(res.ok){
                   fnRenderVitalSign({pat:data,vitalSigns:res.data});
               }
            });
        });

        form.on("select(vitalSignFrame-weekNo)",function(data){
            var weekNo = data.value;
            if(!dateList || dateList.length<=0 || !CurrentRowData) {
                return;
            }
            var param = {
                patientCode: CurrentRowData.code,
                patientCureNo: CurrentRowData.cureNo
            };
            var pat = CurrentRowData;
            if(weekNo && !isNaN(weekNo)) {
                var startIndex = (weekNo - 1) * 7;
                var endIndex = startIndex + 6;
                if (endIndex >= dateList.length) {
                    endIndex = dateList.length - 1;
                }
                param.opDateFrom = dateList[startIndex].substring(0,10);
                param.opDateTo = dateList[endIndex].substring(0,10);
                pat.weekNo = weekNo;
            }else{
                pat.weekNo = undefined;
            }

            http.post('vitalSign/getVitalSignList',param,function(res){
                if(res.ok){
                    fnRenderVitalSign({pat:pat,vitalSigns:res.data});
                }
            });
        });
    });

</script>